package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.*;
import com.hdax.utils.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 *
 * @author Aizen
 * @date 2022/05/04
 */
@Controller
@RequestMapping("/user")
public class EmployeeInfoController {

    /**
     * 员工信息服务
     */
    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 部门服务
     */
    @Autowired
    private DepartmentService departmentService;

    /**
     * 角色服务
     */
    @Autowired
    private RoleService roleService;

    /**
     * 合同文件服务
     */
    @Autowired
    private ContractfileService contractfileService;



    @GetMapping ("/shouye")
    public ModelAndView index() {
        return new ModelAndView("Home");
    }

    /**
     * 这个下面是员工档案
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String list(Model model) {
        model.addAttribute("roles",departmentService.findAll());
        return "archives/EmployeeProfiles";
    }

    /**
     * 列表
     * 根据名称模糊查询
     *
     * @param page         页面
     * @param limit        限制
     * @param employeeInfo 员工信息
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam("page")int page, @RequestParam("limit")int limit, @Param("employee") EmployeeInfo employeeInfo){
        List<EmployeeInfo> employeeList = employeeInfoService.findAll((page-1)*limit,limit,employeeInfo);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        //查询总条数(该名称下)
        map.put("count", employeeInfoService.findCount());
        //集合
        map.put("data", employeeList);
        return map;
    }

    /**
     * 根据ID查询到员工，编辑员工信息
     *
     * @param id id
     * @return {@link ModelAndView}
     */
    @GetMapping(path = "/edit")
    public ModelAndView edit(@RequestParam("empId") Integer id) {
        //根据ID查到员工信息
        EmployeeInfo employeeInfo = employeeInfoService.findEmployeeInfoById(id);

        //查询所有的部门名称
        List<Department> department = departmentService.findAll();
        //查询所有的职业名称
        List<Role> roles = roleService.roels(employeeInfo.getDepartmentId());
        //查询所有的合同文件
        List<Contractfile> conList = contractfileService.findAll();
        List<Contractfile> conInfoList = (List<Contractfile>)contractfileService.findConAndEmployee2(employeeInfo.getId()).getData();

        ModelAndView mav = new ModelAndView("archives/showEmployeeInfo");
        mav.addObject("roles",roles);
        mav.addObject("department",department);
        mav.addObject("employeeInfo",employeeInfo);
        mav.addObject("conList",conList);
        mav.addObject("conInfoList",conInfoList);
        return mav;
    }

    /**
     * 编辑
     * 提交编辑
     *
     * @param employeeInfo 员工信息
     * @return {@link ResultUtil}
     */
    @PostMapping(path = "/edit")
    @ResponseBody
    public ResultUtil edit(EmployeeInfo employeeInfo) {
        return employeeInfoService.editEmployeeInfo(employeeInfo);
    }

    /**
     * 添加员工档案
     *
     * @return {@link ModelAndView}
     */
    @ResponseBody
    @GetMapping(path = "/insert")
    public ModelAndView insert() {
        ModelAndView modelAndView = new ModelAndView("archives/insert");
        //查询所有的部门
        List<Department> department = departmentService.findAll();
        //查询所有的职务
        List<Role> roles = roleService.findAll();
        List<Contractfile> contractfileList = contractfileService.findAll();
        //所有角色
        modelAndView.addObject("roles",roles);
        //所有部门
        modelAndView.addObject("department",department);
        //所有合同
        modelAndView.addObject("contractfileList",contractfileList);
        return modelAndView;
    }


    /**
     * 提交添加
     *
     * @param employeeInfo 员工信息
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @PostMapping(path = "/insert")
    @ResponseBody
    public ResultUtil insert(EmployeeInfo employeeInfo) {
        return employeeInfoService.insert(employeeInfo);
    }


}
